#include "asm_utils.h"
#include "interrupt.h"
#include "stdio.h"
#include "program.h"
#include "thread.h"
#include "sync.h"

// 屏幕IO处理器
STDIO stdio;
// 中断管理器
InterruptManager interruptManager;
// 程序管理器
ProgramManager programManager;

Semaphore chopsticks[5];

void delay()
{
    int delay = 0xdffffff;
    while (delay)
        --delay;
}

void philosopher(void *arg)
{
    int num = programManager.running->pid;
    while(1)
    {
        printf("A philosopher %d is thinking...\n", num);

        delay();
        chopsticks[num-1].P();
        chopsticks[(num) % 5].P();
        printf("A philosopher %d is eating...\n", num);

        delay();
        chopsticks[num-1].V();
        chopsticks[(num) % 5].V();
    }
}

void first_thread(void *arg)
{
    // 第1个线程不可以返回
    stdio.moveCursor(0);
    for (int i = 0; i < 25 * 80; ++i) {
        stdio.print(' ');
    }
    stdio.moveCursor(0);

    for(int i = 0; i < 5; i++) chopsticks[i].initialize(1);

    for(int i = 0; i <5; i++)
        programManager.executeThread(philosopher, nullptr, "philosopher", 1);

    asm_halt();
}

extern "C" void setup_kernel()
{

    // 中断管理器
    interruptManager.initialize();
    interruptManager.enableTimeInterrupt();
    interruptManager.setTimeInterrupt((void *)asm_time_interrupt_handler);

    // 输出管理器
    stdio.initialize();

    // 进程/线程管理器
    programManager.initialize();


    // 创建第一个线程
    int pid = programManager.executeThread(first_thread, nullptr, "first thread", 1);
    if (pid == -1)
    {
        printf("can not execute thread\n");
        asm_halt();
    }


    ListItem *item = programManager.readyPrograms.front();
    PCB *firstThread = ListItem2PCB(item, tagInGeneralList);
    firstThread->status = RUNNING;
    programManager.readyPrograms.pop_front();
    programManager.running = firstThread;
    asm_switch_thread(0, firstThread);

    asm_halt();
}
